package com.telecom.weixi;

import java.util.ArrayList;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.telecom.weixi.net.RespListener;
import com.telecom.weixi.net.RespResult;
import com.telecom.weixi.net.WebServiceEngine;
import com.telecom.weixi.parse.OweInfo;
import com.telecom.weixi.parse.OweInfoParse;
import com.telecom.weixi.parse.TermDetailParse;
import com.telecom.weixi.parse.TerminalDetailInfo;
import com.telecom.weixi.parse.XmlParse;
import com.telecom.weixi.util.Utils;
import com.telecom.weixi.view.TitleView;

public class TermSearchActivity extends BaseActivity implements RespListener {

	private int mType;

	public static final int Type_Term = 0x01;
	public static final int Type_Owe = 0x02;
	public static int PAGE_SIZE = 13;
	private int currentPage = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.term_search);
		initView();
		initWaitDialog();

		String strTitle = getString(R.string.search_term);
		int type = getIntent().getIntExtra(TopMenuActivity.TYPE_TAG, TopMenuActivity.TYPE_TERM_SEARCH);
		mType = Type_Term;
		switch (type) {
		case TopMenuActivity.TYPE_TERM_SEARCH:
			mType = Type_Term;
			strTitle = getString(R.string.search_term);
			break;
		case TopMenuActivity.TYPE_OWE_SEARCH:
			mType = Type_Owe;
			strTitle = getString(R.string.search_owe);
			break;
		default:
		}
		title.setTitle(strTitle);
	}

	private TitleView title;
	private EditText search;

	private void initView() {
		title = (TitleView) findViewById(R.id.title_view);
		title.setPreButtonListener(backListener);

		findViewById(R.id.choose).setOnClickListener(searchListener);
		search = (EditText) findViewById(R.id.input_content);
	}

	private void hideSI() {
		((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(TermSearchActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
	}

	ProgressDialog waitDialog;

	public void initWaitDialog() {
		if (waitDialog == null) {
			waitDialog = new ProgressDialog(this);
			waitDialog.setMessage(getResources().getString(R.string.search_and_wait));
			waitDialog.setCancelable(true);
		}
	}

	public void showWait() {
		waitDialog.show();
	}

	public void stopWait() {
		if (waitDialog != null && waitDialog.isShowing()) {
			waitDialog.hide();
		}
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		if (waitDialog != null) {
			waitDialog.dismiss();
		}
		super.onStop();
	}

	View.OnClickListener backListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			TermSearchActivity.this.finish();
		}
	};

	String mSearchNbr;
	View.OnClickListener searchListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			hideSI();
			mSearchNbr = search.getText().toString();
			if (mSearchNbr.length() == 0) {
				Utils.showErrorDialog(TermSearchActivity.this, TermSearchActivity.this.getString(R.string.warning_search), TermSearchActivity.this.getString(R.string.promp_confirm), null);
				return;
			}
			showWait();
			WebServiceEngine ws = new WebServiceEngine();
			ws.setRespListener(TermSearchActivity.this);
			if (mType == Type_Term) {
				ws.getTerminalInfodetail(WeixiApplication.getApplication().getUserId(), mSearchNbr, PAGE_SIZE, currentPage);
			} else {
				ws.getOweInfo(WeixiApplication.getApplication().getUserId(), mSearchNbr);
			}
		}
	};

	@Override
	public void OnDataRecv(RespResult resp) {
		// TODO Auto-generated method stub
		stopWait();
		switch (resp.getReqType()) {
		case GetTerminalInfodetail:
			dealTermSearch(resp);
			break;
		case GetOweInfo:
			dealOweSearch(resp);
			break;
		}

	}

	private void dealTermSearch(RespResult resp) {
		TermDetailParse parseHelper = new TermDetailParse();
		XmlParse.parse(Utils.String2InputStream(resp.toString()), parseHelper);

		ArrayList<TerminalDetailInfo> info = parseHelper.getData();
		if (info != null && info.size() > 0) {
			Intent intent = new Intent(TermSearchActivity.this, TermActivity.class);
			Bundle data = new Bundle();
			data.putString(TermActivity.Tag_Term_Nbr, mSearchNbr);
			data.putParcelableArrayList(TermActivity.Tag_Term_Info, info);
			data.putString("userId", WeixiApplication.getApplication().getUserId());
			data.putString("searchNbr", mSearchNbr);
			data.putInt("pageSize", PAGE_SIZE);
			data.putInt("pageNo", currentPage);
			intent.putExtras(data);
			startActivityForResult(intent, WeixiApplication.CODE_REQ);
		} else {
			Utils.showErrorDialog(this, getString(R.string.error_search), getString(R.string.promp_confirm), null);
		}
	}

	private void dealOweSearch(RespResult resp) {
		OweInfoParse parseHelper = new OweInfoParse();
		XmlParse.parse(Utils.String2InputStream(resp.toString()), parseHelper);
		ArrayList<OweInfo> info = parseHelper.getData();
		if (info != null && info.size() > 0) {
			Intent intent = new Intent(TermSearchActivity.this, OweBoardActivity.class);
			Bundle data = new Bundle();
			data.putParcelableArrayList(OweBoardActivity.Tag_Owe_Info, info);
			intent.putExtras(data);
			startActivityForResult(intent, WeixiApplication.CODE_REQ);
		} else if (info != null && info.size() == 0) {
			Utils.showErrorDialog(this, getString(R.string.empyt_owe_data), getString(R.string.promp_confirm), null);
		} else {
			Utils.showErrorDialog(this, getString(R.string.error_search), getString(R.string.promp_confirm), null);
		}
	}
}
